package com.priox.sigar.infraestructura.services;


import com.mm.support.mapper.DTOMapperService;
import com.priox.sigar.client.dto.AcreedorDTO;
import com.priox.sigar.client.dto.ClienteDTO;
import com.priox.sigar.client.flt.AcreedorFlt;
import com.priox.sigar.client.flt.ClienteFlt;
import com.priox.sigar.domain.model.*;
import com.priox.sigar.domain.service.AcreedorService;
import com.priox.sigar.domain.service.ClienteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Johann
 * Date: 28/08/13
 * Time: 11:13 PM
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ClienteServiceImpl implements ClienteService {

    @Autowired
    ClienteRepository clienteRepository;

    @Autowired
    SociedadRepository sociedadRepository;

    @Autowired
    private DTOMapperService mapperService;

    public List<Cliente> findByIds(List<Long> ids) {
        return clienteRepository.findByIds(ids);
    }

    public List<ClienteDTO> findAll() {
        List<Cliente> cadenas = clienteRepository.findAll();
        List<ClienteDTO> resultado = new ArrayList<ClienteDTO>();

        for (Cliente model : cadenas) {
            ClienteDTO dto = mapperService.map(model, ClienteDTO.class);
            resultado.add(dto);
        }
        return resultado;
    }

    public List<Cliente> findAllCadenas() {
        return clienteRepository.findAll();
    }

    public List<ClienteDTO> findByFilter(ClienteFlt filtro) {
        return clienteRepository.findByFilter(filtro);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void save(ClienteDTO cadenaDTO, boolean updateMode) {
        Cliente entity = mapperService.map(cadenaDTO, Cliente.class);
        if (updateMode) {
            clienteRepository.merge(entity);
        } else {
            clienteRepository.persist(entity);
        }
//        acreedorRepository.saveAcreedorSociedad(entity.getAcreedorId(), new ArrayList<SociedadDTO>(cadenaDTO.getSociedades()));

    }

    @Transactional
    public void saveSimple(ClienteDTO cadenaDTO, boolean updateMode) {

        Cliente entity = mapperService.map(cadenaDTO, Cliente.class);

        if (updateMode) {
            clienteRepository.merge(entity);
        } else {
            clienteRepository.persist(entity);
        }
    }

    @Transactional
    public void update(ClienteDTO anterior) {
        Cliente cadena = mapperService.map(anterior, Cliente.class);
        clienteRepository.merge(cadena);
    }


    @Transactional
    public List<Cliente> findByRUC(String ruc) {
        List<Cliente> list = new ArrayList<Cliente>();
        if (ruc != null) {
            list = clienteRepository.findByRUC(ruc);
        }
        return list;
    }

    public void delete(ClienteDTO nuevoRegistro) {
//        clienteRepository.delete(nuevoRegistro.getId());
    }

    public void executeSP() {
        clienteRepository.executeSP();
    }
}
